package java8._03_Lambda;

import org.junit.Test;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

public class _02_方法引用2 {


    public static void main(String[] args) {

        List<String> strList = Arrays.asList("a", "b", "A", "B");


        Comparator<String> comparator = Comparator.comparing(String::toUpperCase);
        strList.sort(comparator.reversed());

        strList.sort(comparator.thenComparing(comparator));

        System.out.println(strList);

        BiPredicate<List<String>, String> contains = (list, element) -> list.contains(element);
        BiPredicate<List<String>, String> contains2 = List::contains;


        Function<String, Integer> stringToInteger = (String s) -> Integer.parseInt(s);
        Function<String, Integer> stringToInteger2 = Integer::parseInt;

        Supplier<String> supplier = () -> new String("dd");
        Supplier<String> supplier2 = new String("dd")::toLowerCase;


        Consumer<String> consumer = (String::valueOf);
        consumer.andThen(String::valueOf);


        Supplier<String> one = String::new;
                         one = () -> new String();

        UnaryOperator<String> oper = String::new;
                              oper = str -> new String(str);

        BiFunction<byte[], Charset, String> fun = String::new;
                                            fun = (bytes, charset) -> new String(bytes, charset);



        Predicate<String> pd = str -> str.startsWith("xx");
        Predicate<String> pd2 = str -> str.endsWith("oo");
        Predicate<String> pd3 = pd.and(pd2);
        Predicate<String> pd4 = pd.or(pd2);


    }


    @Test
    public void testAndThen() {

        Function<Integer, Integer> add2 = num -> num + 2;
        Function<Integer, Integer> mult2 = num -> num * 2;

        Function<Integer, Integer> addAndMult = add2.andThen(mult2);
        Function<Integer, Integer> addComMult = add2.compose(mult2);

        System.out.println(addAndMult.apply(10)); // 24
        System.out.println(addComMult.apply(10)); // 22

        System.out.println("-------------------华丽分割线----------------------");
        UnaryOperator<String> oper = String::new;
        Function<String, Integer> strToInt = Integer::parseInt;
        Function<Integer, String> intToStr = Object::toString;

        Function<String, Integer> andThen = oper.andThen(strToInt);
        Function<Integer, String> compose = oper.compose(intToStr);
        Integer apply1 = andThen.apply("100");
        System.out.println(apply1);
        String apply = compose.apply(200);
        System.out.println(apply);


    }
}
